package com.khshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.Order;
import com.khshop.mapper.OrderMapper;
import com.khshop.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.khshop.vo.ServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Override
    public ServiceResult<IPage<Order>> searchOrderByStatus(Integer status, Integer page, Integer limit) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        Page<Order> iPage = new Page<Order>(page, limit);
            queryWrapper = queryWrapper.eq("status",status);
        IPage<Order> orderIPage = orderMapper.selectPage(iPage, queryWrapper);
        return ServiceResult.of(orderIPage,orderIPage.getSize()>0);
    }

    @Override
    public ServiceResult<Order> addOrder(Order order) {
        return ServiceResult.of(orderMapper.insert(order)>0);
    }

    @Override
    public ServiceResult<Order> closeOrder(Long id) {
        Order order=new Order();
        order.setId(id);
        order.setStatus(0);
        return ServiceResult.of(orderMapper.updateById(order)>0);
    }

    @Override
    public ServiceResult<Order> cancelOrder(Long id) {
        Order order=new Order();
        order.setId(id);
        order.setStatus(0);
        //退款
        return ServiceResult.of(orderMapper.updateById(order)>0);
    }

    @Override
    public ServiceResult<Order> cancelAndReturnOrder(Long id) {
        Order order=new Order();
        order.setId(id);
        order.setStatus(0);
        //退货退款
        return ServiceResult.of(orderMapper.updateById(order)>0);
    }

    @Override
    public ServiceResult<Order> changeOrderStatus(Long id, Integer status) {
        Order order=new Order();
        order.setId(id);
        order.setStatus(status);
        return ServiceResult.of(orderMapper.updateById(order)>0);
    }
}
